//
//  InteractionButton.m
//  IntelligenceCommunity
//
//  Created by zhangpei on 16/1/5.
//  Copyright © 2016年 zhangpei. All rights reserved.
//

#import "InteractionButton.h"
#import "ViewHelper.h"
#import "PublicMethod.h"
#import "UtilityConfig.h"

@interface InteractionButton ()
{
    UIImageView *imageView;

    UILabel *titleLabel;
}

@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *colors;
@property (nonatomic, strong) NSArray *images;

@end

@implementation InteractionButton

@synthesize titles,colors,images;

- (instancetype)initWithFrame:(CGRect)frame withTitles:(NSArray *)titleAry withTitleColors:(NSArray *)colorAry withImages:(NSArray *)imageAry
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        titles = titleAry;
        colors = colorAry;
        images = imageAry;
        [self initView];
    }
    return self;
}

- (void)initView
{
    UIImage *normal = [UIImage imageNamed:[images firstObject]];

    imageView = [ViewHelper superView:self withFrame:CGRectMake(0, (self.bounds.size.height - 13)/2, 13*normal.size.width/normal.size.height, 13) withImage:normal withBackgroundColor:[UIColor clearColor] withUserInteractionEnabled:YES];
    

    titleLabel = [ViewHelper superView:self withFrame:self.bounds withText:[titles firstObject] withBackgroundColor:[UIColor clearColor] withTextColor:[colors firstObject] withFont:[UIFont boldSystemFontOfSize:12] withTextAlignment:NSTextAlignmentCenter];
    
    [self setState:InteractionButtonStateNormal];
}

- (void)refreshShowView
{
    if (_state == InteractionButtonStateSelected)
    {
        imageView.image = [UIImage imageNamed:[images lastObject]];
        titleLabel.text = [titles lastObject];
        titleLabel.textColor = [colors lastObject];
    }
    else
    {
        imageView.image = [UIImage imageNamed:[images firstObject]];
        titleLabel.text = [titles firstObject];
        titleLabel.textColor = [colors firstObject];
    }
    
    [titleLabel sizeToFit];
    
    imageView.frame = CGRectMake((self.bounds.size.width - imageView.bounds.size.width - titleLabel.bounds.size.width - 3)/2, (self.bounds.size.height - imageView.bounds.size.height)/2, 13*imageView.image.size.width/imageView.image.size.height, 13);
    
    titleLabel.frame = CGRectMake(imageView.frame.origin.x +imageView.frame.size.width + 3, (self.bounds.size.height - titleLabel.frame.size.height)/2, titleLabel.bounds.size.width, titleLabel.bounds.size.height);
}

- (void)setState:(InteractionButtonState)state
{
    
    _state = state;
    
    [self refreshShowView];
}

- (InteractionButtonState)state
{
    return _state;
}

- (void)showNoIconButtonWithTitle:(NSString *)title
{
    titleLabel.text = title;
    
    [titleLabel sizeToFit];
    
    imageView.frame = CGRectMake((self.bounds.size.width - imageView.bounds.size.width - titleLabel.bounds.size.width - 3)/2, (self.bounds.size.height - imageView.bounds.size.height)/2, 0, 13);
    
    titleLabel.frame = CGRectMake((self.bounds.size.width - titleLabel.bounds.size.width)/2, (self.bounds.size.height - titleLabel.frame.size.height)/2, titleLabel.bounds.size.width, titleLabel.bounds.size.height);

}

@end
